using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Data;

namespace Avinor.NET.Client.Windows
{
    public class AvinorViewModel : INotifyPropertyChanged
    {
        ICollectionView _airportsView;
        ObservableCollection<Airport> _airports;
        string _filter;

        public ObservableCollection<Airport> Airports
        {
            get
            {
                return _airports;
            }
        }

        public string Filter
        {
            get { return _filter; }
            set { 
                _filter = value.ToLowerInvariant();
                _airportsView.Filter = airport =>  ((Airport)airport).Name.ToLowerInvariant().Contains(Filter);
                OnPropertyChanged("Filter");
            }
        }

        public AvinorViewModel()
        {
            _airports = new ObservableCollection<Airport>(new AvinorWebClient().GetAirports());
            _airportsView = CollectionViewSource.GetDefaultView(_airports);

        }
        
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}